package com.syqc.monitor.ui.photo

import android.content.Context
import android.view.KeyEvent
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.github.chrisbanes.photoview.PhotoView
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.loadStringDImgError
import com.syqc.comlib.utils.setResultTo
import com.syqc.entity.PhotoInfo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityPhotoDetailBinding
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys


/**
 *@Author Jasper
 *@Time   2020/12/10
 *@Desc   照片详情
 */
class PhotoDetailActivity :
    BaseBarActivity<ActivityPhotoDetailBinding>(ActivityPhotoDetailBinding::inflate) {
    private var index = 0

    override fun initView() {
        index = intent.getIntExtra(Keys.INTENT_OTHER, 0)
        val title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        val data: ArrayList<PhotoInfo> = DataUtil.mPhotoInfo!!
        val toolbarText =
            ToolbarHelper.setToolbarView(this, title, "${index + 1} / ${data.size}")

        initRecyclerView(data, toolbarText)

    }

    private fun initRecyclerView(
        data: ArrayList<PhotoInfo>,
        toolbarText: TextView
    ) {
        val layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        val mAdapter = PhotoDetailAdapter(this)
        binding.recyclerPhoto.layoutManager = layoutManager
        binding.recyclerPhoto.adapter = mAdapter
        binding.recyclerPhoto.scrollToPosition(index)
        mAdapter.setList(data)

        //https://www.jianshu.com/p/f23f6271a074
        val pagerSnapHelper = PagerSnapHelper()
        pagerSnapHelper.attachToRecyclerView(binding.recyclerPhoto)

        //滑动事件
        binding.recyclerPhoto.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    val view: View = pagerSnapHelper.findSnapView(layoutManager) ?: return
                    val position = layoutManager.getPosition(view)
                    if (index == position) return
                    index = position
                    toolbarText.text = "${index + 1} / ${data.size}"
                }
            }
        })
    }

    override fun setListener() {

    }

    inner class PhotoDetailAdapter(private val mContext: Context) :
        BaseQuickAdapter<PhotoInfo, BaseViewHolder>(R.layout.item_detail_photo) {
        private val unit = mContext.getString(R.string.unit_speed)
        override fun convert(holder: BaseViewHolder, item: PhotoInfo) {
            item.run {
                holder.getView<PhotoView>(R.id.pv_detail_photo)
                    .loadStringDImgError(mContext, fullLink)
                holder.setText(R.id.tv_detail_alarm, reason)
                    .setText(R.id.tv_detail_speed_time, "$speed $unit   $time")
                    .setText(R.id.tv_detail_address, addr)
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        setResultTo(this) {
            putExtra(Keys.INTENT_OTHER, index)
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        setResultTo(this) {
            putExtra(Keys.INTENT_OTHER, index)
        }
        return super.onKeyDown(keyCode, event)
    }


}